package Module;

import com.mysql.jdbc.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-12-19
 * Time: 15:43
 * To BE A Better Person-2020-3-2
 */
public class UserDao {
    public User selectByname(String username) {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try{
            connection=DButil.getConnection();
            String sql="select * from user where username = ?;";
            statement=connection.prepareStatement(sql);
            statement.setString(1,username);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                User user=new User();
                user.setUserid(resultSet.getInt("userid"));
                user.setPassword(resultSet.getString("password"));
                user.setUsername(resultSet.getString("username"));
                user.setUseraddre(resultSet.getString("useraddre"));
                return user;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(connection,statement,resultSet);
        }
        return null;
    }
    public User selectByid(int userid) {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try{
            connection=DButil.getConnection();
            String sql="select * from user where userid = ?;";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,userid);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                User user=new User();
                user.setUserid(resultSet.getInt("userid"));
                user.setPassword(resultSet.getString("password"));
                user.setUsername(resultSet.getString("username"));
                user.setUseraddre(resultSet.getString("useraddre"));
                return user;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            assert connection != null;
            assert statement != null;
            assert resultSet != null;
            DButil.close(connection,statement,resultSet);
        }
        return null;
    }

    public void addUser(User user){
        Connection connection=null;
        PreparedStatement statement=null;
        try{
            connection=DButil.getConnection();
            String sql="insert into user values (?,?,?,?,?);";
            statement= connection.prepareStatement(sql);
            String userid= String.valueOf(user.getUserid());
            statement.setInt(1,0);
            statement.setString(2,userid);
            statement.setString(3,user.getUsername());
            statement.setString(4,user.getPassword());
            statement.setString(5,user.getUseraddre());
            int ret=statement.executeUpdate();
            if(ret == 1){
                System.out.println("插入成功");
            }else {
                System.out.println("插入失败");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DButil.close(connection,statement,null);
        }
    }

    public int countPass(User user){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection=DButil.getConnection();
            String sql="select count(*) as total from blog where userid = ?;";
            statement=connection.prepareStatement(sql);
            statement.setString(1,String.valueOf(user.getUserid()));
            resultSet=statement.executeQuery();
            resultSet.next();
            int ret= resultSet.getInt("total");
            return ret;
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DButil.close(connection,statement,resultSet);
        }
        return 0;
    }


}
